package com.example.android_test1;

import android.os.Looper;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

public class MainActivity extends AppCompatActivity {
        private String username="";
        private String password="";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        login(username,password);
        }

        public void login(final String use, final String pwd){
            new Thread(new Runnable() {
                @Override
                public void run() {
                    String path="http://8.129.1.249:8080/AndroidHttp_war/mustLogin?logname="+use+"&password="+pwd;
                    try {
                        try{
                            URL url = new URL(path); //新建url并实例化
                            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
                            connection.setRequestMethod("GET");//获取服务器数据
                            connection.setReadTimeout(8000);//设置读取超时的毫秒数
                            connection.setConnectTimeout(8000);//设置连接超时的毫秒数
                            InputStream in = connection.getInputStream();
                            BufferedReader reader = new BufferedReader(new InputStreamReader(in));


                            String result = reader.readLine();//读取服务器进行逻辑处理后页面显示的数据
                            Log.d("MainActivity","run: "+result);
                            if (result.equals("login successfully!")){
                                Log.d("MainActivity","run2: "+result);
                                Looper.prepare();
                                Log.d("MainActivity","run3: "+result);
                                Toast.makeText(MainActivity.this,"You logined successfully!",Toast.LENGTH_SHORT).show();
                                Log.d("MainActivity","run4: "+result);
                                Looper.loop();
                            }
                        }catch (MalformedURLException e){
                            e.printStackTrace();
                        }
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }).start();
        }

}
